package com.example.da42;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.da20.OpenAiChat;
import com.example.da20.TestConfig;
import jakarta.annotation.Resource;
import org.example.Easy2LearnApplication;
import org.example.domain.Question;
import org.example.domain.QuestionTranslated;
import org.example.excel.DataDAO;
import org.example.excel.ExcelListener;
import org.example.mapper.QuestionMapper;
import org.example.mapper.QuestionTranslatedMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest(classes = {Easy2LearnApplication.class, TestConfig.class})
public class Translate {

    @Resource
    private DataDAO<Question> questionDataDAO;

    @Resource
    private QuestionMapper questionMapper;

    @Test
    public void test(){
        ExcelListener<Question> listener = new ExcelListener<>(questionDataDAO);
        EasyExcel.read("C:\\Users\\84206\\Desktop\\42.xlsx", Question.class, listener).sheet().doRead();
        List<Question> list = questionDataDAO.getList();
        questionMapper.insert(list);
    }

    @Resource
    private OpenAiChat openAiChat;

    @Resource
    private QuestionTranslatedMapper questionTranslatedMapper;

    //第1和第500没有做
    @Test
    public void test1(){
        openAiChat.setModel("glm-4-air");
        openAiChat.clearHistory();;
        openAiChat.setSystemPrompt("现在要给你一段中文，你给我回复他翻译成英文的样子。");
        openAiChat.setAssistantRole("你是一个中英翻译，同时也是一位奥地利钻石飞机维修技师，你对奥地利钻石DA42飞机和中英文都很精通，你能够根据我给你的中文翻译成英文");
//        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>());
//        for (int i = 501; i < questionList.size(); i++) {
            Question q = questionMapper.selectById(134763);
            String question = q.getQuestion();
            String item1 = q.getItem1();
            String item2 = q.getItem2();
            String item3 = q.getItem3();
            String item4 = q.getItem4();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("请将下面这段中文翻译成英文，只需要回复我翻译后的结果，其他不需要回复\n");
            stringBuilder.append(question)
                    .append("\n").append("A.").append(item1)
                    .append("\n").append("B.").append(item2)
                    .append("\n").append("C.").append(item3)
                    .append("\n").append("D.").append(item4);
            System.out.println(stringBuilder);
            System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
//            System.out.println("进行到第"+i+"个");
            String chat = openAiChat.chat(String.valueOf(stringBuilder));
            System.out.println(chat);
            System.out.println("==========================================================");
            q.setTranslated( chat);
            questionMapper.updateById(q);
//        }
    }


    @Test
    public void test2(){
        Question q = questionMapper.selectById(134693);
        String translated = q.getTranslated();
        String[] split = translated.split("\n");
        QuestionTranslated questionTranslated = new QuestionTranslated();
        questionTranslated.setQuestionId(q.getId());
        questionTranslated.setQuestion(split[0]);
        questionTranslated.setItem1(split[1].replace("A.",""));
        questionTranslated.setItem2(split[2].replace("B.",""));
        questionTranslated.setItem3(split[3].replace("C.",""));
        questionTranslated.setItem4(split[4].replace("D.",""));
        questionTranslated.setAnswer(q.getAnswer());
        System.out.println(questionTranslated);
    }

    @Test
    public void test3(){
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>());
        StringBuilder builder = new StringBuilder();
        int index =1;
        for (Question q : questionList) {
            String translated = q.getTranslated();
            String[] split = translated.split("\n");
            if (split.length!=5) {
                System.out.println("error");
            }
            builder.append(index+":"+q.getQuestion()+"          id:"+q.getId())
                    .append("\n")
                    .append(index+":"+split[0])
                    .append("\n")
                    .append("A."+q.getItem1())
                    .append("\n")
                    .append(split[1])
                    .append("\n")
                    .append("B."+q.getItem2())
                    .append("\n")
                    .append(split[2])
                    .append("\n")
                    .append("C."+q.getItem3())
                    .append("\n")
                    .append(split[3])
                    .append("\n")
                    .append("D."+q.getItem4())
                    .append("\n")
                    .append(split[4])
                    .append("\n")
                    .append("答案："+q.getAnswer())
                    .append("\n")
            .append("\n");
            index++;
        }
        System.out.println(builder);
    }
}
